package com.tbit.uqbike.client.service.impl;

import com.alibaba.fastjson.JSON;
import com.tbit.uqbike.client.constant.AccountConfigExtConstant;
import com.tbit.uqbike.client.constant.MachineStateConstant;
import com.tbit.uqbike.client.constant.RedisConstant;
import com.tbit.uqbike.client.dao.core.AccountGifDao;
import com.tbit.uqbike.client.dao.core.MachineFunctionDao;
import com.tbit.uqbike.client.exception.BaseException;
import com.tbit.uqbike.client.pojo.Machine;
import com.tbit.uqbike.client.pojo.MachineFunction;
import com.tbit.uqbike.client.pojo.TerLastStatus;
import com.tbit.uqbike.client.pojo.newEntity.AccountConfigExt;
import com.tbit.uqbike.client.pojo.newEntity.AccountGif;
import com.tbit.uqbike.client.service.AccountGifService;
import com.tbit.uqbike.client.service.CacheService;
import com.tbit.uqbike.client.service.MachineService;
import com.tbit.uqbike.client.service.RedisService;
import com.tbit.uqbike.client.util.ObjectUtil;
import com.tbit.uqbike.client.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: chen
 * @Description: 区域GIF图片配置
 * @Date: 创建于 10:47 2023/5/24
 */
@Service
@Slf4j
public class AccountGifServiceImpl implements AccountGifService {

    @Autowired
    private AccountGifDao accountGifDao;
    @Autowired
    private MachineService machineService;
    @Autowired
    private MachineFunctionDao machineFunctionDao;
    @Autowired
    private RedisService redisService;
    @Autowired
    private CacheService cacheService;

    @Override
    public List<AccountGif> getByAccountId(String userCode) {
        Machine machine = machineService.getByUserCode(userCode);
        if (machine == null) {
            throw new BaseException("-30005", "设备不存在");
        }
        AccountConfigExt accountConfigExt = cacheService.getAccountConfigExtByKey(machine.getAccountId(), AccountConfigExtConstant.ACCOUNTCONFIG_BUTTON_GIF);
        if (accountConfigExt == null || "0".equals(accountConfigExt.getParamValue())) {
            return null;
        }
        List<MachineFunction> machineFunctions = machineFunctionDao.getAll(machine.getMachineId());
        if (machineFunctions.isEmpty()) {
            return null;
        }
        /**是否需要返回GIF图片*/
        boolean checkHelmet = false;
        for (MachineFunction machineFunction : machineFunctions) {
            switch (machineFunction.getFunctionType()) {
                case MachineStateConstant.MACHINE_TYPE_HELMAT:
                    checkHelmet = checkHe(machine.getMachineNO());
                    break;
                case MachineStateConstant.MACHINE_TYPE_INTEL_HELMET:
                    checkHelmet = checkHe(machine.getMachineNO());
                    break;
                case MachineStateConstant.MACHINE_TYPE_BLE_HELMET:
                    checkHelmet = checkHe(machine.getMachineNO());
                    break;
                default:
                    break;
            }

        }
        if (checkHelmet) {
            return accountGifDao.getByAccountId(machine.getAccountId());
        }
        return null;
    }

    /**
     * 头盔校验
     *
     * @param machineNO
     * @return
     */
    public boolean checkHe(String machineNO) {
        /**通过缓存查询状态，头盔关闭不返回*/
        String json = redisService.getMain(RedisUtil.getTerHashId(machineNO), RedisConstant.REDIS_TER_LAST_STATUS);
        if ((json != null && json.length() > 0)) {
            TerLastStatus terLastStatus = JSON.parseObject(json, TerLastStatus.class);
            Integer terStatus = terLastStatus.getCarStatus();
            if (!ObjectUtil.statusParse(11, Integer.valueOf(terStatus))) {
                return false;
            }
        }
        /**借车前插销状态是打开的不返回*/
        String value = redisService.get(RedisConstant.MACHINE_BORROW_EX + machineNO);
        if (value != null) {
            Integer borrowValue = Integer.valueOf(value);
            if (!ObjectUtil.statusParse(3, borrowValue)) {
                return false;
            }
        }

        return true;
    }

}
